package pa165.dominatingspecies.dao.common;


import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import pa165.dominating.species.entities.Animal;
import pa165.dominating.species.entities.Element;
import pa165.dominating.species.entities.Environment;
import pa165.dominating.species.entities.User;
import pa165.dominating.species.enums.ElementType;
import pa165.dominating.species.enums.UserType;
import pa165.dominating.species.tos.AnimalTo;
import pa165.dominating.species.tos.ElementTo;
import pa165.dominating.species.tos.EnvironmentTo;
import pa165.dominating.species.tos.UserTO;

/**
 * @author Tomas Kalla
 */
public class TestUtil {

    private static final String NAME = "name";
    private static final String SPECIES = "species";
    private static final String DESCRIPTION = "description";
    private static final String LOGIN = "Login";
    private static final String PASSWORD = "secret";
    private static final ElementType ELEMENT_TYPE = ElementType.BUSH;
    private static final String ELEMENT_TYPE_STRING = "water";
    public static final Long ID = 1L;
    private static final UserType USER_TYPE = UserType.ADMIN;

    private TestUtil() {
        throw new AssertionError("Static class. Cannot be instantiated.");
    }

    public static Animal createNewAnimal() {
        final Animal animal = new Animal();
        animal.setName(NAME);
        animal.setSpecies(SPECIES);
        for (int i = 0; i < 2; i++) {
            animal.addPrey(createExistingAnimal());
        }
        animal.setEnvironment(createExistingEnvironment());
        return animal;
    }

    public static Animal createExistingAnimal() {
        final Animal animal = createNewAnimal();
        animal.setId(ID);
        return animal;
    }

    public static Environment createNewEnvironment() {
        final Environment environment = new Environment();
        environment.setDescription(DESCRIPTION);
        environment.setName(NAME);
        for (int i = 0; i < 3; i++) {
            environment.addElement(createExistingElement());
        }
        return environment;
    }

    public static Environment createExistingEnvironment() {
        final Environment environment = createNewEnvironment();
        environment.setId(ID);
        return environment;
    }

    public static Answer<Void> setIdToEnviroment(final Long id) {
        return new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                Environment environment = (Environment) invocation.getArguments()[0];
                environment.setId(id);
                return null;
            }
        };
    }

    public static Element createNewElement() {
        final Element element = new Element();
        element.setDescription(DESCRIPTION);
        element.setType(ELEMENT_TYPE);
        return element;
    }

    public static Element createExistingElement() {
        final Element element = createNewElement();
        element.setId(ID);
        return element;
    }

    public static User createNewUser() {
        final User user = new User();
        user.setName(NAME);
        user.setLogin(LOGIN);
        user.setPassword(PASSWORD);
        user.setType(USER_TYPE);
        return user;
    }

    public static User createExistingUser() {
        final User user = createNewUser();
        user.setId(ID);
        return user;
    }

    public static AnimalTo createNewAnimalTo() {
        final AnimalTo animalTo = new AnimalTo();
        animalTo.setName(NAME);
        animalTo.setSpecies(SPECIES);
        for (int i = 0; i < 2; i++) {
            animalTo.addPrey(createExistingAnimalTo());
        }
        animalTo.setEnvironmentTo(createExistingEnvironmentTo());
        return animalTo;
    }

    public static AnimalTo createExistingAnimalTo() {
        final AnimalTo animalTo = createNewAnimalTo();
        animalTo.setId(ID);
        return animalTo;
    }

    public static EnvironmentTo createNewEnvironmentTo() {
        final EnvironmentTo environmentTo = new EnvironmentTo();
        environmentTo.setDescription(DESCRIPTION);
        environmentTo.setName(NAME);
        for (int i = 0; i < 3; i++) {
            environmentTo.addElement(createExistingElementTO());
        }
        return environmentTo;
    }

    public static EnvironmentTo createExistingEnvironmentTo() {
        final EnvironmentTo environmentTo = createNewEnvironmentTo();
        environmentTo.setId(ID);
        return environmentTo;
    }

    public static ElementTo createNewElementTO() {
        final ElementTo elementTo = new ElementTo();
        elementTo.setDescription(DESCRIPTION);
        elementTo.setType(ELEMENT_TYPE);
        return elementTo;
    }

    public static ElementTo createExistingElementTO() {
        final ElementTo elementTo = createNewElementTO();
        elementTo.setId(ID);
        return elementTo;
    }

    public static UserTO createNewUserTO() {
        final UserTO userTO = new UserTO();
        userTO.setName(NAME);
        userTO.setLogin(LOGIN);
        userTO.setPassword(PASSWORD);
        userTO.setType(USER_TYPE);
        return userTO;
    }

    public static UserTO createExistingUserTO() {
        final UserTO userTO = createNewUserTO();
        userTO.setId(ID);
        return userTO;
    }
}
